package jumpingnotes.dao.hibernate;

import java.util.List;
import java.lang.Long;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import jumpingnotes.dao.SurveyDAO;
import jumpingnotes.model.Survey;

public class SurveyHibernateDAO extends GenericHibernateDAO<Survey> implements SurveyDAO
{
	public SurveyHibernateDAO()
	{
		super(Survey.class);
	}

	
	@SuppressWarnings("unchecked")
	public List<Survey> findByUuid(final String uuid) {
		return (List<Survey>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(final Session session) throws HibernateException
			{
				final Query query = session.createQuery("from Survey s where s.surveyUuid = :uuid")
					.setString("uuid", uuid);								
				return query.list();
			}
		}, true);
	}
	
	@SuppressWarnings("unchecked")
	public List<Survey> findAllSurveyee() {
		return (List<Survey>) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("from Survey s where s.surveyName = 'name'");								
				return query.list();
			}
		}, true);
	}
	
	public Long getSurveyCount(final String name, final String value) {
		return (Long) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("select count(*) from Survey s where s.surveyName = :name and s.surveyValue like :value")
					.setString("name", name).setString("value", "%" + value + "%");								
				return query.uniqueResult();
			}
		}, true);
	}
}
